package uiLayer;

import java.util.ArrayList;
import java.util.Scanner;

import controllerLayer.CustomerCtr;
import controllerLayer.EmployeeCtr;
import controllerLayer.ProductCtr;
import controllerLayer.SaleCtr;

import modelLayer.Customer;
import modelLayer.Employee;
import modelLayer.SaleLinesItem;

public class TimberUI {
	private ArrayList<SaleLinesItem> sls;
	private String empId;
	private String custId;
	private static TimberUI instance = null;
	private EmployeeCtr ectr;
	private CustomerCtr cctr;
	private ProductCtr pctr;
	private SaleCtr sctr;
	private Employee employee;
	private boolean logged;
	
	public TimberUI() {
		sls = new ArrayList<SaleLinesItem>();
		empId = null;
		custId = null;
		ectr = new EmployeeCtr();
		cctr = new CustomerCtr();
		pctr = new ProductCtr();
		sctr = new SaleCtr();
		logged = false;
	}
	public static TimberUI getInstance(){
		if(instance==null){
			instance = new TimberUI();
		}
		return instance;
		}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TimberUI instance = new TimberUI();
		instance.run();

	}
	public void login(){
		Scanner keyboard = new Scanner(System.in);
		System.out.println("Employee CPR:");
		empId = keyboard.nextLine();
		employee = ectr.getEmployee(empId);
		logged = true;
	}
	public void run(){
		int id = 0;
		if(!logged)login();
		Scanner keyboard = new Scanner(System.in);
		System.out.println("Customer CPR:");
		custId = keyboard.nextLine();
		Customer customer = cctr.getCustomer(custId);
		sls = typeProducts();
		if(employee!=null&&customer!=null&&sls!=null){id = sctr.createOffer(customer,employee,sls);
		System.out.println("ID of your offer is: " + id);
		}
		try{
			acceptOffer(custId,id);
		}
		catch(NullPointerException npe){
			System.out.println("Something went wrong!");
			OfficeUI.getInstance().run();
		}
		}
	

	private void printOrder(int id) {		
		pctr.sellProduct(empId, custId, sls);
		System.out.println("Order:"+sctr.findSale(id).printOffer(id));
	}
	
	private void printDispatchNote(int id){
		System.out.println("Dispatch Note"+sctr.findSale(id).printDispatchNote(id));
	}
	
	private void printDeliveryNote(int id){
		System.out.println("Delivery Note"+sctr.findSale(id).printDeliveryNote(id));
	}
	
	private ArrayList<SaleLinesItem> typeProducts() {
		ArrayList<SaleLinesItem> ss = new ArrayList<SaleLinesItem>();
		Scanner keyboard = new Scanner(System.in);
		boolean finish = false;
		while(!finish){
		System.out.println("Sale Lines Item ID: ");
		String ID = keyboard.nextLine();
		System.out.println("Quantity:");
		double quantity = InputParser.getInstance().parseDouble(keyboard.nextLine());
		
		try{
		pctr.countQuantity(ID, quantity);
		}
		
		catch(NullPointerException npe ){
			System.out.println("Item doesn't exist");
			OfficeUI.getInstance().run();}
		
			catch (IllegalArgumentException iae){
				System.out.println(iae.getMessage());
				TimberUI.getInstance().run();
			}
				
		

		ss.add(pctr.getSaleLinesItem(ID));
		System.out.println("if you want to add new product press 1, if not press 0");
		if(keyboard.nextLine().equals("0"))finish = true;
		}
		try{
		return ss;
		}
		catch(NullPointerException npe){
			return null;
		}
		}
	
	public void acceptOffer(String custId,int id){
		Scanner keyboard = new Scanner(System.in);
		
		System.out.println(sctr.findSale(id).printOffer(id));
		System.out.println("Accept Offer?");
		System.out.println("1.Yes");
		System.out.println("2.No");
		if(keyboard.nextLine().equals("1")){	
			printOrder(id);
			printDispatchNote(id);
			printDeliveryNote(id);
		}
	
	}
	}

